﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;

namespace MikeHole.TileMaker
{
    public partial class TileMakerControl : UserControl
    {
        double initialAngle;
        double initialScale;

        public TileMakerControl()
        {
            InitializeComponent();
        }

        private void GestureListener_Hold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {

        }

        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            bordertransform.TranslateX = imagetransform.TranslateX = 0;
            bordertransform.TranslateY = imagetransform.TranslateY = 0;
            bordertransform.Rotation = imagetransform.Rotation = 0;
            bordertransform.ScaleX = imagetransform.ScaleX = 1;
            bordertransform.ScaleY = imagetransform.ScaleY = 1;
        }

        private void GestureListener_DragStarted(object sender, Microsoft.Phone.Controls.DragStartedGestureEventArgs e)
        {
        }

        private void GestureListener_DragDelta(object sender, Microsoft.Phone.Controls.DragDeltaGestureEventArgs e)
        {
            bordertransform.TranslateX = imagetransform.TranslateX += e.HorizontalChange;
            bordertransform.TranslateY = imagetransform.TranslateY += e.VerticalChange;
        }

        private void GestureListener_DragCompleted(object sender, Microsoft.Phone.Controls.DragCompletedGestureEventArgs e)
        {
        }

        private void GestureListener_PinchStarted(object sender, Microsoft.Phone.Controls.PinchStartedGestureEventArgs e)
        {
            initialAngle = imagetransform.Rotation;
            initialScale = imagetransform.ScaleX;
        }

        private void GestureListener_PinchDelta(object sender, Microsoft.Phone.Controls.PinchGestureEventArgs e)
        {
            bordertransform.Rotation = imagetransform.Rotation = initialAngle + e.TotalAngleDelta;
            bordertransform.ScaleY = bordertransform.ScaleX = imagetransform.ScaleX = imagetransform.ScaleY = initialScale * e.DistanceRatio;
        }

        private void GestureListener_PinchCompleted(object sender, Microsoft.Phone.Controls.PinchGestureEventArgs e)
        {
        }

        public BitmapImage GetCurrentTile()
        {
            WriteableBitmap wb = new WriteableBitmap(imageSource, null);
            BitmapImage bi = new BitmapImage();

            MemoryStream ms = new MemoryStream(); 

            wb.SaveJpeg(ms, 173, 173, 0, 100);
   
            BitmapImage bmp = new BitmapImage();

            bmp.SetSource(ms);

            return bmp;
        }


        public void SetTileImage(BitmapImage imageToSet)
        {
            tileImage.Source = imageToSet;
        }


        public void SetBackground(SolidColorBrush solidColorBrush)
        {
            imageSource.Background = LayoutRoot.Background = solidColorBrush;
        }
    }
}
